<script>
import { ref } from "vue";
export default {
  setup() {
    //地址数据
    let address = ref([]);
    //添加地址
    let addaddress = (data) => {
      if (
        !window.localStorage.getItem("address") ||
        window.localStorage.getItem("address") == "[]"
      ) {

        if (window.localStorage.getItem("address") == "[]") {
          window.localStorage.removeItem("address");
        }
        address.value=[]
        // console.log("没有值");
        // 直接存入数据
        address.value.push(data);
        // console.log(address.value);
        // address.value.push({data})
        window.localStorage.setItem("address", JSON.stringify(address.value));
      } else {
        // //取出值id+1再push
        let one = JSON.parse(window.localStorage.getItem("address"));
        // console.log(one[0].id);
        // // console.log("数组长度==>",one.length-1);
        let id = one[one.length - 1].id + 1;
        data.id = id;
        one.push(data);
        // console.log(one);
        window.localStorage.setItem("address", JSON.stringify(one));
      }
      // console.log(address,data);
    };

    return {
      //地址信息
      addaddress,
      address,
    };
  },
};
</script>
<template>
  <router-view @addaddress="addaddress"></router-view>
</template>

<style>
</style>
